using UnityEngine;
using UnityEngine.UIElements;
using Yoozoo.UI.YGUI;
using XLua;
using ListView = Yoozoo.UI.YGUI.ListView;

public class ListViewTabRedDots : MonoBehaviour
{
    [SerializeField] public YButton m_leftRedDotsButton;
    [SerializeField] public YButton m_lightRedDotsButton;
    [SerializeField] public bool isNotListView = false;
    private ListView m_bindListViewObj;
    private LuaFunction m_calculateLeftRedDots;
    private LuaFunction m_calculateRightRedDots;
    
    private LuaFunction m_jumpLeftRedDotsFunc;
    private LuaFunction m_jumpRightRedDotsFunc;
    
    private int jumpLeftIndex = -1;
    private int jumpRightIndex = -1;
    // ListView onScroll 的时候 计算一下当前可视范围内覆盖着的index范围
    // lua方法给到 判断 index 左右范围外 是否有红点要显示
    public void Awake()
    {
        if (isNotListView)
        {
            // 没有用ListView的，后续再新增吧，先业务层处理
            var listViewObj = GetComponent<ScrollView>();
        }
        else
        {
            var listViewObj = GetComponent<ListView>();
            if (listViewObj)
            {
                m_bindListViewObj = listViewObj;
                listViewObj.luaCalculateLeftRedDots += calculateLeftRedDotsFunc;
                listViewObj.luaCalculateRightRedDots += calculateRightRedDotsFunc;
            
                // listViewObj.jumpLeftRedDots += jumpLeftRedDotsFunc;
                // listViewObj.jumpRightRedDots += jumpRightRedDotsFunc;

                m_leftRedDotsButton.onClick.AddListener(jumpLeftRedDotsFunc);
                m_lightRedDotsButton.onClick.AddListener(jumpRightRedDotsFunc);
            }
        }
        
    }
    
    public void OnDestroy()
    {
        if (m_bindListViewObj)
        {
            m_bindListViewObj.luaCalculateLeftRedDots -= calculateLeftRedDotsFunc;
            m_bindListViewObj.luaCalculateRightRedDots -= calculateRightRedDotsFunc;
            
            // m_bindListViewObj.jumpLeftRedDots -= jumpLeftRedDotsFunc;
            // m_bindListViewObj.jumpRightRedDots -= jumpRightRedDotsFunc;
            m_leftRedDotsButton.onClick.RemoveListener(jumpLeftRedDotsFunc);
            m_lightRedDotsButton.onClick.RemoveListener(jumpRightRedDotsFunc);
        }
    }
    
    private void jumpLeftRedDotsFunc()
    {
        if (jumpLeftIndex >= 0 && m_bindListViewObj != null)
        { 
            // 活动商业化界面 要求跳转到中间，一列最多可看到三个多一点，再往前看一个所以减1
            // 后续如果有其他功能要用到，可以在这里开放一个偏移参数 （主要懒得算中间位Index了）
            m_bindListViewObj.ScrollToView(Mathf.Max(0, jumpLeftIndex-1));
            if (m_jumpLeftRedDotsFunc != null)
            {
                m_jumpLeftRedDotsFunc.Func<int, int>(jumpLeftIndex);
            }
        }
    }
    
    private void jumpRightRedDotsFunc()
    {
        if (jumpRightIndex >= 0 && m_bindListViewObj != null)
        { 
            m_bindListViewObj.ScrollToView(Mathf.Max(0, jumpRightIndex-1));
            if (m_jumpRightRedDotsFunc != null)
            {
                m_jumpRightRedDotsFunc.Func<int, int>(jumpRightIndex);
            }
        }
    }
    
    private bool calculateLeftRedDotsFunc(int index)
    {
        bool isShow = false;
        
        int  leftIndex = -1;
        if (m_calculateLeftRedDots != null)
        {
            leftIndex = m_calculateLeftRedDots.Func<int, int>(index);
            jumpLeftIndex = leftIndex;
        }

        isShow = leftIndex >= 0;
        m_leftRedDotsButton.gameObject.SetActive(isShow);
        return isShow;
    }
    
    private bool calculateRightRedDotsFunc(int index)
    {
        bool isShow = false;
        
        int  rightIndex = -1;
        if (m_calculateRightRedDots != null)
        {
            rightIndex = m_calculateRightRedDots.Func<int, int>(index);
            jumpRightIndex = rightIndex;
        }

        isShow = rightIndex >= 0;
        m_lightRedDotsButton.gameObject.SetActive(isShow);
        return isShow;
    }

    public void SetCalculateLeftRedDotsLuaFunc(LuaFunction func)
    {
        this.m_calculateLeftRedDots = func;
    }
    
    public void SetCalculateRightRedDotsLuaFunc(LuaFunction func)
    {
        this.m_calculateRightRedDots = func;
    }
    
    public void SetJumpLeftRedDotsFunc(LuaFunction func)
    {
        this.m_jumpLeftRedDotsFunc = func;
    }
    
    public void SetJumpRightRedDotsFunc(LuaFunction func)
    {
        this.m_jumpRightRedDotsFunc = func;
    }

    public void refreshBtn()
    {
        
    }
}